/*
 * @Descripttion: 0
 * @version: 1.0
 * @Author: 郝汉森
 * @Date: 2020-08-24 18:50:00
 * @LastEditors: 王娆
 * @LastEditTime: 2020-08-27 21:04:29
 */
import {getQuestionsType,addQuestionsType,getSubject,getExamType,addQuestions,getQuestions,editQuestions} from "../../api/question"
import {Message} from "element-ui"
const state = {
    // 所有的试题类型
    questionsType: [],
    // 所有的课程
    subject: [],
    // 所有的考试类型
    examType: [],
    // 试题
    questions:[]
}

const mutations = {
    // 获取所有的试题类型
    getQuestionsType(state, data) {
        state.questionsType = data
    },
    // 获取所有的课程
    getSubject(state, data) {
        state.subject = data
    },
    // 获取所有的考试类型
    getExamType(state, data) {
        state.examType = data
    },
    // 获取试题
    getQuestions(state,data){
        state.questions= data
    }
}

const actions = {
    // 获取所有的试题类型
    async getQuestionsTypeApi({commit}) {
        const { data: res } = await getQuestionsType()
        if(res.code!==1){
            this.$message.error(res.msg)
        }
        commit('getQuestionsType',res.data)
    },
    // 获取所有的课程
    async getSubjectApi({commit}) {
        const { data: res } = await getSubject()
        if(res.code!==1){
            this.$message.error(res.msg)
        }
        commit('getSubject',res.data)
    },
    // 获取所有的考试类型
    async getExamTypeApi({commit}) {
        const { data: res } = await getExamType()
        if(res.code!==1){
            this.$message.error(res.msg)
        }
        commit('getExamType',res.data)
    },
    // 添加试题类型
    async addQuestionsTypeApi({dispatch},data){
        const {data:res} = await addQuestionsType(data)
        if(res.code!==1){
            Message.error('添加失败')
        }
        Message.success(res.msg)
        dispatch('getQuestionsTypeApi')
    },
    // 添加试题
    async addQuestionsApi({dispatch},data){
        const {data:res} = await addQuestions(data)
        console.log(res)
        return res.code
    },
    // 获取试题
    async getQuestionsApi({commit},data){
        const {data:res} = await getQuestions(data)
        console.log(res)
        if(res.code!==1){
            this.$message.error(res.msg)
        }
        commit('getQuestions',res.data)
    },
    // 编辑试题
    async editQuestionsApi(context,data){
        const { data:res}= await editQuestions(data)
        console.log(res)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}